cmake_minimum_required(VERSION 3.8)
project(locking)
set(file_to_lock "${PROJECT_SOURCE_DIR}/file_to_lock")
set(dir_to_lock "${PROJECT_SOURCE_DIR}/dir_to_lock")

file(LOCK ${file_to_lock} RESULT_VARIABLE var)
message(STATUS "lock file - ${var}")

file(LOCK ${dir_to_lock} DIRECTORY RESULT_VARIABLE var)
message(STATUS "lock dir - ${var}")

file(LOCK ${file_to_lock} RELEASE)

function(func)
    file(LOCK ${file_to_lock} GUARD FUNCTION RESULT_VARIABLE var)
    message(STATUS "lock file FUNCTION - ${var}")
endfunction()

func()

file(LOCK ${file_to_lock} GUARD PROCESS RESULT_VARIABLE var)
message(STATUS "lock file PROCESS - ${var}")
file(LOCK ${file_to_lock} RESULT_VARIABLE var)
message(STATUS "lock file again - ${var}")

file(LOCK ${file_to_lock} RELEASE)

file(LOCK ${file_to_lock} RESULT_VARIABLE var TIMEOUT 0)
message(STATUS "lock file TIMEOUT 0 - ${var}")

file(LOCK ${file_to_lock} RELEASE)

file(LOCK ${file_to_lock} RESULT_VARIABLE var TIMEOUT 3)
message(STATUS "lock file TIMEOUT 3 - ${var}")

file(LOCK ${file_to_lock} RELEASE)
file(LOCK ${file_to_lock} GUARD FILE RESULT_VARIABLE var)
message(STATUS "lock file FILE - ${var}")
